var express = require('express')
var { graphqlHTTP } = require('express-graphql')
const { buildSchema } = require('graphql')

// String, Int, Float, Boolean, ID
// type 定义的就是 对象
// [类型]  [Int]代表整型数组
/*
    type Query {
        rollDice(numDice: Int!, numSides:Int!): [Int]
    }
*/
var schema = buildSchema(`
  type User {
    name: String!,
    age: Int,
    sex: Boolean,
    department: String
  }

  type Query {
    hello: String,
    accountName: String,
    friends: [ User ],
  }
`)

var root = {
  name: () => '李四',
  hello: () => 'hello world',
  accountName: () => '张三',
  User: () => ({ name: '李四' }),
  friends: () => [
    { name: '李四', age: '12', sex: 0, department: '科学部' },
    { name: '李四', age: '12', sex: 0, department: '科学部' },
  ],
}
// 查询语句
/*
{
  hello
  accountName
  friends{
    name
    age
    sex
  }
}

*/
var app = express()

app.get('/', (req, res) => {
  res.end('<a href="/graphql">graphql</a>')
})

app.use(
  '/graphql',
  graphqlHTTP({
    schema: schema,
    rootValue: root,
    graphiql: true, // 是否开启调试界面，true是开启，false是不开启
  })
)

app.listen(4000, () => console.log('server is run on http://localhost:4000'))
